<?php

require_once( 'mvc/view.php' );

require_once( 'flexical/admin/message.php' );

class Flexical_Admin_Tab_Info
{
	public $title;
	public $request_type;

	public function __construct( $title = null, $request_type = null )
	{
		$this->title = $title;
		$this->request_type = $request_type;
	}
}

class Flexical_Admin_Form_Table_Info
{
	public $request_type;
	public $submit_button_text;
	public $cancel_enabled = false;
	public $rows = array();
	public $hiddens = array();
}

abstract class Flexical_Admin_View extends MVC_View
{
	public function render( $response )
	{
		$this->render_header();
		$this->render_messages( $response );
		$this->render_title();
		$this->render_tab_bar();
		$this->render_details( $response );
		$this->render_footer();
	}

	private function render_header()
	{
		echo( '<div class="wrap">' . "\n" );
	}

	public function get_messages( $response )
	{
		$messages = array();

		return $messages;
	}

	private function render_messages( $response )
	{
		foreach ( $this->get_messages( $response ) as $message )
		{
			$this->render_message( $message );
		}
	}

	private $types = null;

	private function init_types()
	{
		$this->types = array(
			Flexical_Admin_Message::$FATAL   => 'error',
			Flexical_Admin_Message::$ERROR   => 'error',
			Flexical_Admin_Message::$WARNING => 'updated',
			Flexical_Admin_Message::$INFO    => 'updated',
			Flexical_Admin_Message::$DEBUG   => 'updated highlight',
		);
	}

	private function render_message( $message = null )
	{
		if ( $this->types == null )
		{
			$this->init_types();
		}

		$message_class = isset( $this->types[ $message->type ] ) ? $this->types[ $message->type ] : 'error';

		$p_start = substr( $message->content, 0, 1 ) == '<' ? '' : '<p>';
		$p_end   = substr( $message->content, 0, 1 ) == '<' ? '' : '</p>';

		echo( "<div id=\"message\" class=\"" . $message_class . "\">" . $p_start . $message->content . $p_end . "</div>\n" );
	}

	private function render_title()
	{
		echo( '<div id="icon-options-general" class="icon32"></div><h2>Flexical Settings</h2>' );
	}

	public $ABOUT_TAB = 'about';
	public $FEEDS_TAB = 'feed';
	public $QUERIES_TAB = 'query';
	public $FORMATS_TAB = 'format';
	public $LISTINGS_TAB = 'listing';

	public function get_selected_tab()
	{
		return null;
	}

	private $tabs = null;

	private function initialize_tabs()
	{
		$this->tabs = array(
			$this->ABOUT_TAB => new Flexical_Admin_Tab_Info( 'About', 'show_about' ),
			$this->FEEDS_TAB => new Flexical_Admin_Tab_Info( 'Feeds', 'show_feeds' ),
			$this->QUERIES_TAB => new Flexical_Admin_Tab_Info( 'Queries', 'show_queries' ),
			$this->FORMATS_TAB => new Flexical_Admin_Tab_Info( 'Formats', 'show_formats' ),
			$this->LISTINGS_TAB => new Flexical_Admin_Tab_Info( 'Listings', 'show_listings' ),
		);
	}

	private function render_tab_bar()
	{
		if ( $this->tabs == null )
		{
			$this->initialize_tabs();
		}

		$tabs = array();
		$selected_tab = $this->get_selected_tab();

		foreach ( $this->tabs as $tab_key => $tab_info )
		{
			$style = $tab_key == $selected_tab ? 'font-weight: bold; background-color: black; color: white;' : '';

			$tabs[] = "<form action=\"" . $this->get_request_url( $tab_info->request_type ) . "\" method=\"post\">" .
			"<input type=\"submit\" value=\"" . $tab_info->title . "\" style=\"font-size: 16px; " . $style . "\" /></form>";
		}

		echo( "<table><tr><td>" . implode( "</td><td>", $tabs ) . "</td></tr></table><hr />\n" );
	}

	public abstract function render_details( $response );

	private function render_footer()
	{
		echo( "</div> <!-- wrap -->\n" );
	}

	public function get_request_url( $request_type = null, $params = array() )
	{
		$args = array( 'request_type=' . $request_type );

		foreach ( $params as $key => $value )
		{
			$args[] = $key . '=' . rawurlencode( $value );
		}

		return wp_nonce_url( get_admin_url() . 'options-general.php?page=flexical&' . implode( '&', $args ), $request_type );
	}

	public function render_form_table( $info = null )
	{
		echo( "<form action=\"" . $this->get_request_url( $info->request_type ) . "\" method=\"post\">\n" );
		echo( "<table class=\"form-table\">\n" );

		foreach ( $info->rows as $row_label => $row_value )
		{
			echo( "<tr scope=\"row\"><th valign=\"top\" align=\"left\">" . $row_label . "</th><td valign=\"top\" align=\"left\">" . $row_value . "</td></tr>\n" );
		}

		echo( "<tr><th></th><td valign=\"top\" align=\"left\"><input class=\"button-primary\" type=\"submit\" name=\"submit_selected\" value=\"" . $info->submit_button_text . "\" />" );

		if ( $info->cancel_enabled )
		{
			echo( "&nbsp; <input type=\"submit\" name=\"cancel_selected\" value=\"Cancel\" />" );
		}

		echo( "</td></tr>\n" );
		echo( "</table>\n" );

		foreach ( $info->hiddens as $key => $value )
		{
			echo( "<input type=\"hidden\" name=\"" . $key . "\" value=\"" . esc_html( $value ) . "\" />" );
		}

		echo( "</form>\n" );
	}

	public function get_confirmation_checkbox()
	{
		return "<input type=\"checkbox\" name=\"confirmed\" value=\"yes\" /> Check this box to confirm this action\n";
	}

	public function get_radio_button_input( $name, $buttons, $checked = null, $end_text = null )
	{
		$options = array();

		foreach ( $buttons as $value => $label )
		{
			$input = "<input type=\"radio\" name=\"" . $name . "\" value=\"" . $value . "\" ";
			$input .= ( $checked != null && $value == $checked ) ? "checked " : "";
			$input .= "/> " . $label;

			$options[] = $input;
		}

		return implode( " &nbsp; ", $options ) . ( ( $end_text != null ) ? ( "<br />\n" . $end_text ) : '' );
	}

	public function build_listings_in_use_table( $listings )
	{
		$text .= "<table style=\"border-spacing: 10px;\"><tr><th>ID</th><th>Title</th><th>Actions</th></tr>\n";

		foreach ( $listings as $listing )
		{
			$text .= "<tr><td>" . $listing->id . "</td><td>" . $listing->title . "</td><td>";
			$text .= "<a href=\"" . $this->get_request_url( 'edit_listing', array( 'listing_id' => $listing->id ) ) . "\">Edit</a> | ";
			$text .= "<a href=\"" . $this->get_request_url( 'confirm_delete_listing', array( 'listing_id' => $listing->id ) ) . "\">Delete</a></td></tr>\n";
		}

		$text .= "</table>\n";

		return $text;
	}
}

?>
